//
//  UIViewController+YPContoller.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/28.
//

#import "UIViewController+YPContoller.h"
#import "BaseWebController.h"

@implementation UIViewController (YPContoller)

+ (UIViewController *)getCurrentViewController {
    UIViewController* currentViewController = [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else {
                break;
            }
        }
    }
    
    return currentViewController;
}

+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

- (UIViewController *)uacb_topmost {
    UIViewController *topmost = self;
    UIViewController *above;
    while ((above = topmost.presentedViewController)) {
        topmost = above;
    }
    return topmost;
}

+ (void)goWebController:(NSString *)titleStr url:(NSString *)url {
    BaseWebController *vc = [[BaseWebController alloc] init];
    vc.titleStr = titleStr;
    vc.urlStr = url;
    [[UIViewController getCurrentViewController].navigationController pushViewController:vc animated:YES];
}

+ (void)removeControllerWithString:(NSString *)vcStr {
    if (vcStr.length <= 0) {
        return;
    }
    NSMutableArray *controllerArr = [UIViewController getCurrentViewController].navigationController.childViewControllers.mutableCopy;
    [controllerArr enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:NSClassFromString(vcStr)]) {
            [controllerArr removeObject:obj];
        }
    }];
    [[UIViewController getCurrentViewController].navigationController setViewControllers:controllerArr];
}

@end
